package cn.tedu.servlet;

import cn.tedu.http.HttpRequest;
import cn.tedu.http.HttpResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.FileTemplateResolver;

import java.io.PrintWriter;
import java.util.Comparator;

/**
 * Author：程Sir
 * Name： SmallServer
 * Content:
 *  用户修改信息的处理类
 */
public class ToUpdateUserServlet {

    public void service(HttpRequest request, HttpResponse response){
        System.out.println("现在可以处理用户信息修改业务了....................");
        // 获取数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nikename = request.getParameter("nikename");
        String age = request.getParameter("age");

        // 创建引擎对象
        TemplateEngine templateEngine = new TemplateEngine();
        // 创建解析器对象
        FileTemplateResolver resolver = new FileTemplateResolver();
        // 指定解析的模板
        resolver.setTemplateMode("html");
        // 指定解析的字符集
        resolver.setCharacterEncoding("UTF-8");
        // 将解析器添加到模板引擎中
        templateEngine.addTemplateResolver(resolver);

        //创建容器对象
        Context context = new Context();
        // 将数据添加进容器中
        context.setVariable("name", username);
        context.setVariable("pwd", password);
        context.setVariable("nikename", nikename);
        context.setVariable("age", age);

        // 让引擎开始整合数据和页面
        String html = templateEngine.process("./webapps/myweb/update.html", context);
        System.out.println("整合后的页面数据是：" + html);

        // 将装配好的字符串页面响应出去
        PrintWriter printWriter = response.getWriter();
        printWriter.println(html);

    }
}
